
<div id="account_center_baseinfo">
    <form class="baseinfo" style="text-align: center; margin-top: 60px;">
        <p>
            <input type="password" name="password" placeholder="密码">
        </p>
        <p>
            <input type="password" name="newpassword" placeholder="输入新密码">
        </p>
        <p>
            <input type="password" name="renewpassword" placeholder="确认新密码">
        </p>
    </form>
    <div>
        <button style="margin-left: 295px; width: 256px;" class="btn btn-default save">保存</button>
    </div>
</div>
<script>
    $(document).ready(function(){
        var $btn = $("#account_center_baseinfo .btn.save");

        $btn.addClass("hide");
        $("input[name='password']").change(function(){
            $btn.addClass("hide");
            var jqxhr = $.post("/login",{
                account: '${session["user"].getStr("username")}',
                password: $("input[name='password']").val()
            }, function(response){
                $btn.removeClass("hide");
            });
            jqxhr.fail(function(){
                console.log(arguments);
                Pinion.simpleNotify({
                    message: "密码不正确"
                });
                $btn.addClass("hide");
            });
        });


        var check = function(){
            if(!$("input[name='newpassword']").val()){
                Pinion.simpleNotify("请填写新密码");
                return false;
            }
            if($("input[name='renewpassword']").val() != $("input[name='newpassword']").val()){
                Pinion.simpleNotify("密码不一致");
                return false;
            }
            return true;
        };



        $("#account_center_baseinfo .btn.save").on("click", function(e){
            if(!check()){
                return
            }
            var formArr = $("form.baseinfo").serializeArray();
            var formJson = {};
            for(var index in formArr){
                formJson[formArr[index].name] = formArr[index].value;
            }
            var jqxhr = $.post("/account/set_password", formJson, function(response){
                Pinion.simpleNotify("修改成功")
            });
            jqxhr.fail(function(){
                Pinion.simpleNotify("修改失败")
            });
        });
    })
</script>